﻿	function checkLoginForm(object){
		
		var html;
		var message;
		message = "";
		html = "";
		if (object.login_id.value == ""){
			message += "Please input Login name\n";
		}
		
		if (object.pwd.value == ""){
			message += "Please input Password\n";
		}
		
		if (message == ""){		
			
			var form_data = {
				login_id: $("#login_id").val(),
				pwd: $("#pwd").val(),
			};
			
			$.ajax({
			type: "POST",
			async: false,
			url: "include/login_process.php",
			data: form_data,
			dataType: "html",
			success: function(html){
				if($.trim(html) == "true"){
					window.location = "index.php"
				}
				else{
					$('#error_message').html(html);
				}
			}
			});
		}

		
	}
	
	function checkCompanyAddForm(object){

		var html;
		var message;
		message = "";
		html = "";
		
		if(object.company_name.value == ""){
			$('#company_name').addClass("error");
			message += "<li>Please input company name.</li>"
		}
		else{
			var form_data = {
				company_name: $("#company_name").val(),
			};
			$.ajax({
				type: "POST",
				url: "../include/checkCompanyName.php",
				async: false,
				data: form_data,
				dataType: "html",
				success: function(html){
					if(html.indexOf("﻿﻿﻿true") == 0){
						$('#company_name').addClass("error");
						message += "<li>Company Name already exists.</li>";
					}
					else{
						$('#company_name').removeClass("error");
					}
				}
			});
		}
		
		if (object.phone_no.value == ""){
			$('#phone_no').addClass("error");
			message += "<li>Please input Phone Number</li>";
		}
		else if(isNaN(object.phone_no.value)){
			$('#phone_no').addClass("error");
			message += "<li>Please input correct contact no.</li>"
		}
		else{
			$('#phone_no').removeClass("error");
		}
		
		if (object.email.value == ""){
			$('#email').addClass("error");
			message += "<li>Please input Email</li>";
		}else if (!isValidEmailAddress(object.email.value)){
			$('#email').addClass("error");
			message += "<li>Please input correct Email address.</li>"
		}
		else{
			$('#email').removeClass("error");
		}

		if (object.address.value == ""){
			$('#address').addClass("error");
			message += "<li>Please input Address</li>";
		}
		else{
			$('#address').removeClass("error");
		}

		if(object.company_desc.value == ""){
			$('#company_desc').addClass("error");
			message += "<li>Please input company address</li>";
		}
		else{
			$('#company_desc').removeClass("error");
		}
		
		if (message == ""){		
			object.submit();
		}
		else{
			$('#message').addClass("ErrorMessage");
			$("#message").html(message);
		}
	}
	
	
	function checkCompanyUpdateForm(object){
		var count;
		var html;
		var message;
		message = "";
		html = "";
		
		coun = 0;
		$('.company_name').each(function(){
			if ($(this).val() != ""){
				count++;
			}
		});
		if(count == 0){
			$(".company_name").addClass("error");
			message = message+ "Please input Product name\n";
		}
		else{
			$('.company_name').each(function(){
				if ($(this).val() != ""){
					var form_data = {
						company_name: $(this).val(),
						cid: $("#cid").val(),
					};
					$.ajax({
						type: "POST",
						url: "../include/checkCompanyName.php",
						async: false,
						data: form_data,
						dataType: "html",
						success: function(html){
							if(html.indexOf("﻿﻿﻿true") == 0){
								$(this).addClass("error");
								message += "<li>Company Name already exists.</li>";
							}
							else{
								$(this).removeClass("error");
							}
						}
					});
				}
			});
		}
		
		if (object.phone_no.value == ""){
			$('#phone_no').addClass("error");
			message += "<li>Please input Phone Number</li>";
		}
		else if(isNaN(object.phone_no.value)){
			$('#phone_no').addClass("error");
			message += "<li>Please input correct contact no.</li>"
		}
		else{
			$('#phone_no').removeClass("error");
		}
		
		if (object.email.value == ""){
			$('#email').addClass("error");
			message += "<li>Please input Email</li>";
		}else if (!isValidEmailAddress(object.email.value)){
			$('#email').addClass("error");
			message += "<li>Please input correct Email address.</li>"
		}
		else{
			$('#email').removeClass("error");
		}

		if (object.address.value == ""){
			$('#address').addClass("error");
			message += "<li>Please input Address</li>";
		}
		else{
			$('#address').removeClass("error");
		}

		$('.company_desc').each(function(){
			if ($(this).val() != ""){
				count++;
			}
		});
		if(count == 0){
			$(".company_desc").addClass("error");
			message = message+ "<li>Please input company description</li>";
		}else{
			$('#company_desc').removeClass("error");
		}
		
		if (message == ""){		
			object.submit();
		}
		else{
			$('#message').addClass("ErrorMessage");
			$("#message").html(message);
		}
	}
	
	
	function checkUpdateForm(object){

		var html;
		var message;
		var form_data;
		
		checkPwd = true;
		
		message = "";
		html = "";
		
		if (object.email.value == ""){
			$('#email').addClass("error");
			message += "<li>Please input Email address.</li>"
		}else if (!isValidEmailAddress(object.email.value)){
			$('#email').addClass("error");
			message += "<li>Please input correct Email address.</li>"
		}
		else{
			form_data = {
				email: object.email.value,
				old_email: object.old_email.value
			};
			
			$.ajax({
			type: "POST",
			url: "../include/checkEmail.php",
			data: form_data,
			dataType: "html",
			async: false,
			success: function(html){
				if($.trim(html) != "true"){
					$('#email').addClass("error");
					message += "<li>"+html+"</li>";
				}
				else{
					$("#email").removeClass("error");
				}
			}
			});
		}
		
		if (object.type.value == ""){
			$('#type').addClass("error");
			message += "<li>Please select user type.</li>"
		}
		else{
			$("#type").removeClass("error");
		}
		
		if (object.contact_no.value != ""){
			if (isNaN(object.contact_no.value)){
				$('#contact_no').addClass("error");
				message += "<li>Please input correct contact no.</li>"
			}
		}
		else{
			$("#contact_no").removeClass("error");
		}
		
		if (message == ""){	
			object.submit();
		}
		else{
			$('#message').addClass("ErrorMessage");
			$("#message").html(message);
		}
		
	}
	
	function checkRegForm(object){
		var html;
		var message;
		var form_data;
		
		checkPwd = true;
		
		message = "";
		html = "";

		if (object.login_id.value == ""){
			$('#login_id').addClass("error");
			message += "<li>Please input login name</li>";
		}
		else{
		
			form_data = {
				login_id: object.login_id.value,
			};
		
			$.ajax({
			type: "POST",
			url: "../include/checkLoginID.php",
			async: false,
			data: form_data,
			dataType: "html",
			success: function(html){
				if($.trim(html) != "true"){
					$('#login_id').addClass("error");
					message += "<li>Login name already exists</li>";
				}
				else{
					$('#login_id').removeClass("error");
				}
			}
			});
		}
		
		if (object.pwd.value == ""){
			$('#pwd').addClass("error");
			message += "<li>Please input Password</li>";
		}
		else{
			$('#pwd').removeClass("error");
		}
		
		if (object.con_pwd.value == ""){
			$('#con_pwd').addClass("error");
			message += "<li>Please input Confirm Password</li>";
		}
		else{
			$('#con_pwd').removeClass("error");
		}

		if(object.con_pwd.value != "" && object.pwd.value != ""){
			if (object.pwd.value != object.con_pwd.value){
				$('#pwd').addClass("error");
				$('#con_pwd').addClass("error");
				message += "<li>Please input correct Password and Confirm Password</li>";
			}
			else{
				$('#pwd').removeClass("error");
				$('#con_pwd').removeClass("error");
			}
		}
		
		if (object.email.value == ""){
			$('#email').addClass("error");
			message += "<li>Please input Email address.</li>"
		}else if (!isValidEmailAddress(object.email.value)){
			$('#email').addClass("error");
			message += "<li>Please input correct Email address.</li>"
		}
		else{
			form_data = {
				email: object.email.value,
			};
			
			$.ajax({
			type: "POST",
			url: "../include/checkEmail.php",
			data: form_data,
			dataType: "html",
			async: false,
			success: function(html){
				alert($.trim(html));
				if($.trim(html) != "true"){
					$('#email').addClass("error");
					message += "<li>"+html+"</li>";
				}
				else{
					$("#email").removeClass("error");
				}
			}
			});
		}
		
		if (object.type.value == ""){
			$('#type').addClass("error");
			message += "<li>Please select user type.</li>"
		}
		else{
			$("#type").removeClass("error");
		}
		
		if (object.contact_no.value != ""){
			if (isNaN(object.contact_no.value)){
				$('#contact_no').addClass("error");
				message += "<li>Please input correct contact no.</li>"
			}
		}
		else{
			$("#contact_no").removeClass("error");
		}
		
		if (message == ""){	
			form_data = {
				login_id: object.login_id.value,
				pwd: object.pwd.value,
				email: object.email.value,
				firstname: object.firstname.value,
				lastname: object.lastname.value,
				contact_no: object.contact_no.value,
				address: object.address.value,
				birthday: object.birthday.value,
				note: object.note.value,
				type: object.type.value
			};
			
			$.ajax({
			type: "POST",
			url: "../include/user_register_process.php",
			data: form_data,
			async: false,
			dataType: "html",
			success: function(html){
				if($.trim(html) == "true"){
					window.location = "../index.php"
				}
				else{
					$('#message').html(html);
				}
			}
			});
		}
		else{
			$('#message').addClass("ErrorMessage");
			$("#message").html(message);
		}
		
	}
	
	function isValidEmailAddress(emailAddress) {
		var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
		return pattern.test(emailAddress);
	}

	function SearchUser(object){
		
		var form_data = {
			search_type: object.search_type.value,
			keyword: object.keyword.value
		};
	
		$.ajax({
			type: "POST",
			url: "../include/search_user_process.php",
			async: false,
			data: form_data,
			dataType: "html",
			success: function(html){
				$("#sarch_result").html($.trim(html));
			}
		});
	}
	
	
	function loadUserDetail(login_id){
		var form_data = {
			login_id: login_id
		};
	
		$.ajax({
			type: "POST",
			url: "../include/search_user_process.php",
			async: false,
			data: form_data,
			dataType: "html",
			success: function(html){
				$("#sarch_result").html($.trim(html));
			}
		});
	}
	
	function checkLoginName(){
		var form_data = {
			login_id: object.value()
		};
	
		$.ajax({
			type: "POST",
			url: "../include/checkLoginID.php",
			async: false,
			data: form_data,
			dataType: "html",
			success: function(html){
				if($.trim(html) != "true"){
					$("#login_msg").val(html);
					$("#login_id").addClass("error");
				}
			}
		});		
	}
		
	function checkCost(n) {
		if(parseFloat(n) != "NaN"){
			return true;
		}
		else{
			return false;
		}
	}
	
	function SearchProduct(object){
		
		var form_data = {
			search_type: object.search_type.value,
			keyword: object.keyword.value
		};
	
		$.ajax({
			type: "POST",
			url: "../include/product_search_process.php",
			async: false,
			data: form_data,
			dataType: "html",
			success: function(html){
				$("#sarch_result").html($.trim(html));
			}
		});
	}
	
	function deleteProduct(pid){
		var answer = confirm("Are you sure you want to delete this product?");
		if (answer){
			var form_data = {
				key: pid
			};
		
			$.ajax({
				type: "POST",
				url: "../include/product_deactive_process.php",
				async: false,
				data: form_data,
				dataType: "html",
				success: function(html){
					if($.trim(html) != "true"){
						alert("Product deleted!");
						window.location.reload();
					}
				}
			});
		}
	}
	
	function deleteDiscount(pid){
		var answer = confirm("Are you sure you want to delete this discount?");
		if (answer){
			var form_data = {
				key: pid
			};
		
			$.ajax({
				type: "POST",
				url: "../include/discount_deactive_process.php",
				async: false,
				data: form_data,
				dataType: "html",
				success: function(html){
					if($.trim(html) != "true"){
						alert("Discount deleted!");
						window.location.reload();
					}
				}
			});
		}
	}
	
	
	function deleteProductCategory(category_id){
		var answer = confirm("Are you sure you want to delete this category?");
		if (answer){
			var form_data = {
				key: category_id
			};
		
			$.ajax({
				type: "POST",
				url: "../include/product_deactive_category_process.php",
				async: false,
				data: form_data,
				dataType: "html",
				success: function(html){
					if($.trim(html) != "true"){
						alert("Product category deleted!");
						window.location.reload();
					}
				}
			});
		}
	}
	
	
	function deactiveUser(key){
		var answer = confirm("Are you sure you want to deactive this user?");
		if (answer){
			var form_data = {
				key: key
			};
		
			$.ajax({
				type: "POST",
				url: "../include/user_deactive_process.php",
				async: false,
				data: form_data,
				dataType: "html",
				success: function(html){
					if($.trim(html) == "true"){
						alert("User deactived!");
						window.location.reload();
					}
				}
			});
		}
	}
	